package com.itown.expo.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import webframe.common.dao.BaseUser;
import webframe.common.helper.ControllerHelper;

import com.itown.expo.model.EntUserInfo;
import com.itown.expo.model.MainDeclGoods;
import com.itown.expo.model.MainGoodsDecl;

public class EntMultiViewController extends MultiViewController {
	@Override
	public ModelAndView home(HttpServletRequest request,
			HttpServletResponse response) {
		EntUserInfo user = (EntUserInfo) ControllerHelper.getUserBySession();
		Map home = new HashMap();
		home.put("user", user);
		home.put("accessTime", new Date());
		home.put("ip", request.getRemoteAddr());
		return new ModelAndView("Home.jsp", "home", home);
	}

	@Override
	public ModelAndView menu(HttpServletRequest request,
			HttpServletResponse response) {
		return new ModelAndView("Menu.jsp");
	}

	@Override
	// 编辑企业用户
	public ModelAndView entUserEdit(HttpServletRequest request,
			HttpServletResponse response) {
		BaseUser entUser = ControllerHelper.getUserBySession();
		Long id = entUser.getId();
		Map map = new HashMap();
		map.put("user", getServiceContainer().getUserService().findEntUserById(
				id));
		return new ModelAndView("EntUserEdit.jsp", "map", map);
	}

	// 企业核销
	public ModelAndView entClearList(HttpServletRequest request,
			HttpServletResponse response) {
		Long id = ControllerHelper.extractId(request);
		MainGoodsDecl decl = getServiceContainer().getCheckBizService()
				.findMainGoodsDeclById(id);
		List<MainDeclGoods> goodsList = getServiceContainer()
				.getCheckBizService().findClearGoodsByDeclNo(decl.getDeclNo());

		List<MainDeclGoods> noClearlist = new ArrayList<MainDeclGoods>();
		List<MainDeclGoods> alreadyClearlist = new ArrayList<MainDeclGoods>();
		for (MainDeclGoods goods : goodsList) {
			if (goods.getGoodsClearItem() == null
					|| goods.getGoodsClearItem().getClearFlag() == null
					|| goods.getGoodsClearItem().getClearFlag().intValue() < 4) {
				noClearlist.add(goods);
			} else if (goods.getGoodsClearItem().getClearFlag().intValue() >= 4) {
				alreadyClearlist.add(goods);
			}
		}

		Map map = new HashMap();
		map.put("decl", decl);
		map.put("noClearlist", noClearlist);
		map.put("alreadyClearlist", alreadyClearlist);
		map.put("appPrefix", getAppPrefix());
		return new ModelAndView("EntClearList.jsp", "map", map);
	}
}
